package cn.yunwei.module.dingtalk;


import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiMessageCorpconversationStatusBarUpdateRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiMessageCorpconversationStatusBarUpdateResponse;
import com.taobao.api.ApiException;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import static com.sun.management.HotSpotDiagnosticMXBean.ThreadDumpFormat.JSON;


/**
 * @author wangbinggui
 */
@Component
public class ModuleDingTalkAccessToken {

    private static String appSecret = "tZ8lzsm79_-_Q73-DZ_IN0e_t3hrD6oRbvYRAYrmfq4MQd5orcGU1CaShqR9EWj9";
    private static String appKey = "dingat3dsbsfspvk7hzn";

    private static String accessToken;

    private static Long taskId;

    @XxlJob("getAccessToken")
    public void getAccessToken() {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.setAppkey(appKey);
            request.setAppsecret(appSecret);
            request.setHttpMethod("GET");

            OapiGettokenResponse response = client.execute(request);
            accessToken = response.getAccessToken();
            System.out.println(response.getBody());
            sendMessage();
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常逻辑
        }
    }

    public void sendMessage() throws ApiException {
        if (accessToken == null || accessToken.isEmpty()) {
            return;
        }
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(2673998586L);
        request.setUseridList("186105442529355949");
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
//        msg.setMsgtype("text");
//        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
//        msg.getText().setContent("test123");
//        request.setMsg(msg);

//        msg.setMsgtype("link");
//        msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
//        msg.getLink().setTitle("杭州MES系统");
//        msg.getLink().setText("分层审核");
//        msg.getLink().setMessageUrl("点击填写");
//        msg.getLink().setPicUrl("https://mes.dingshenggroup.com/");
//        request.setMsg(msg);

        msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
        msg.getActionCard().setTitle("分层审核");
        msg.getActionCard().setMarkdown(
                        "### 分层审核\n" +
                        "<font color='red'>姓名: wangbinggui</font>\n" +
                        "#### 填写时间: 2025-03-12");
        msg.getActionCard().setSingleTitle("点击填写");
        msg.getActionCard().setSingleUrl("https://mes.dingshenggroup.com/");
        msg.setMsgtype("action_card");
        request.setMsg(msg);

        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
        System.out.println(rsp.getBody());
        taskId = rsp.getTaskId();
        //调回调信息
        callback();
    }

    //提交成功返回一个消息
    public void callback() throws ApiException {
        if (accessToken == null || accessToken.isEmpty()) {
            return;
        }

        /**
         * 审批状态颜色参考
         * 已同意 0xFF78C06E
         * 已拒绝 0xFFF65E5E
         * 已撤销 0xFF858E99
         * 待审批 0xFFFF9D46
         */
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/status_bar/update");
        OapiMessageCorpconversationStatusBarUpdateRequest request = new OapiMessageCorpconversationStatusBarUpdateRequest();
        request.setAgentId(2673998586L);
        request.setStatusValue("提交成功");
        request.setStatusBg("0xFF78C06E");
        request.setTaskId(taskId);
        OapiMessageCorpconversationStatusBarUpdateResponse response = client.execute(request, accessToken);
        System.out.println(response.getBody());
    }
}
